/*
 * Copyright (C), 2013-2016, 汇通达网络有限公司
 * FileName: RedisUtil.java
 * Author:   杨振宁
 * Date:     2016-7-6 上午9:26:17
 * Description: //模块目的、功能描述      
 * History: //修改记录
 * <author>      <time>      <version>    <desc>
 * 修改人姓名             修改时间            版本号                  描述
 */
package com.htd.htdapmcommon.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.connection.RedisConnection;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.io.UnsupportedEncodingException;


@Component("apmRedisUtil")
public class RedisUtil {
	
	@Autowired
	protected RedisTemplate<String, Object> redisTemplate;
	
	/**
	 * 判断缓存是否存在
	 * 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param key
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public boolean exists(final String key){
		return (Boolean) redisTemplate.execute(new RedisCallback() {
            public Boolean doInRedis(RedisConnection connection) throws DataAccessException {
            	try {
            		return connection.exists(key.getBytes());
				} finally {
					connection.close();
				}
            }
        });
	}
	
	/**
	 * 存缓存
	 * 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param key
	 * @param value
	 * @param liveTime
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void set(final String key,final String value,final long liveTime){
		 this.redisTemplate.execute(new RedisCallback() {
	            public Long doInRedis(RedisConnection connection) throws DataAccessException {
	            	
	            	try {
	            		 connection.set(key.getBytes(), value.getBytes());
	 	                if (liveTime > 0) {
	 	                    connection.expire(key.getBytes(), liveTime);
	 	                }
	 	                return 1L;
					} finally {
						connection.close();
					}
	            }
	        });
	}
	
	/**
	 * 存缓存
	 * 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param key
	 * @param value
	 * @param liveTime
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void set(final String key,final String value){
		 this.redisTemplate.execute(new RedisCallback() {
	            public Long doInRedis(RedisConnection connection) throws DataAccessException {
	            	try {
	            		connection.set(key.getBytes(), value.getBytes());
		                return 1L;
					} finally {
						connection.close();
					}
	            }
	        });
	}
	
	/**
	 * 获取缓存值
	 * 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param key
	 * @return
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public String get(final String key) {
        return(String)this.redisTemplate.execute(new RedisCallback() {
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                try {
                	byte[] byteValue = connection.get(key.getBytes()); 
                	if(null==byteValue || 0==byteValue.length){
                		return "";
                	}
                    return new String(byteValue, "utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }finally {
                	connection.close();
                }
                return null;
                
            }
        });
    }
	
	
	/**
	 * 清除指定缓存
	 * 功能描述: <br>
	 * 〈功能详细描述〉
	 *
	 * @param key
	 * @see [相关类/方法](可选)
	 * @since [产品/模块版本](可选)
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public void delete(final String key){
		if(exists(key)){
			 this.redisTemplate.execute(new RedisCallback() {
	            public String doInRedis(RedisConnection connection) throws DataAccessException {
	            	try {
	            		 connection.del(key.getBytes());
	 	                return null;
					} finally  {
						connection.close();
					}
	               
	            }
	        });
		}
	}

	public RedisTemplate<String, Object> getRedisTemplate() {
		return redisTemplate;
	}

	public void setRedisTemplate(RedisTemplate<String, Object> redisTemplate) {
		this.redisTemplate = redisTemplate;
	}
	
	

}
